home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / FCALL.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  10KB  |  375 lines

  1.  
  2. /********************************************
  3. fcall.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13.  
  14. /*$Log:    fcall.c,v $
  15.  * Revision 3.3.1.1  91/09/14  17:23:08  brennan
  16.  * VERSION 1.0
  17.  * 
  18.  * Revision 3.3  91/08/13  06:51:13  brennan
  19.  * VERSION .9994
  20.  * 
  21.  * Revision 3.2  91/06/28  04:16:32  brennan
  22.  * VERSION 0.999
  23.  * 
  24.  * Revision 3.1  91/06/07  10:27:18  brennan
  25.  * VERSION 0.995
  26.  * 
  27.  * Revision 2.1  91/04/08  08:22:59  brennan
  28.  * VERSION 0.97
  29.  * 
  30. */
  31.  
  32. #include "mawk.h"
  33. #include "symtype.h"
  34. #include "code.h"
  35.  
  36. /* This file has functions involved with type checking of
  37.    function calls
  38. */
  39.  
  40. static  FCALL_REC *PROTO(first_pass, (FCALL_REC *) ) ;
  41. static  CA_REC    *PROTO(call_arg_check, (FBLOCK *, CA_REC *,
  42.         INST *, unsigned) ) ;
  43. static  int PROTO(arg_cnt_ok, (FBLOCK *,CA_REC *, unsigned) ) ;
  44.  
  45.  
  46. static int check_progress ;
  47.     /* flag that indicates call_arg_check() was able to type
  48.        check some call arguments */
  49.  
  50. /* type checks a list of call arguments,
  51.    returns a list of arguments whose type is still unknown
  52. */
  53. static CA_REC *call_arg_check( callee, entry_list , start,  line_no)
  54.   FBLOCK *callee ;
  55.   CA_REC *entry_list  ;  
  56.   INST  *start ; /* to locate patch */
  57.   unsigned line_no ; /* for error messages */
  58. { register CA_REC *q ;
  59.   CA_REC *exit_list  = (CA_REC *) 0 ;
  60.  
  61.   check_progress = 0 ;
  62.  
  63.   /* loop :
  64.        take q off entry_list
  65.        test it
  66.            if OK  zfree(q)  else put on exit_list
  67.   */
  68.      
  69.   while ( q = entry_list )
  70.   {
  71.     entry_list = q->link ;
  72.  
  73.     if ( q->type == ST_NONE )
  74.     { /* try to infer the type */
  75.       /* it might now be in symbol table */
  76.       if ( q->sym_p->type == ST_VAR )
  77.       { /* set type and patch */
  78.         q->type = CA_EXPR ;
  79.         start[q->call_offset+1].ptr  = (PTR) q->sym_p->stval.cp ;
  80.       }
  81.       else
  82.       if ( q->sym_p->type == ST_ARRAY )
  83.       { q->type = CA_ARRAY ;
  84.         start[q->call_offset].op = A_PUSHA ;
  85.         start[q->call_offset+1].ptr = (PTR) q->sym_p->stval.array ;
  86.       } 
  87.       else /* try to infer from callee */
  88.       {
  89.         switch( callee->typev[q->arg_num] )
  90.         {
  91.           case  ST_LOCAL_VAR :
  92.                 q->type = CA_EXPR ;
  93.                 q->sym_p->type = ST_VAR ;
  94.                 q->sym_p->stval.cp = new_CELL() ;
  95.                 q->sym_p->stval.cp->type = C_NOINIT ;
  96.                 start[q->call_offset+1].ptr  = 
  97.                          (PTR) q->sym_p->stval.cp ;
  98.                 break ;
  99.  
  100.           case  ST_LOCAL_ARRAY :
  101.                 q->type = CA_ARRAY ;
  102.                 q->sym_p->type = ST_ARRAY ;
  103.                 q->sym_p->stval.array = new_ARRAY() ;
  104.                 start[q->call_offset].op = A_PUSHA ;
  105.                 start[q->call_offset+1].ptr = 
  106.                       (PTR) q->sym_p->stval.array ;
  107.                 break ;
  108.         }
  109.       }
  110.     }
  111.     else
  112.     if ( q->type == ST_LOCAL_NONE )
  113.     { /* try to infer the type */
  114.       if ( * q->type_p == ST_LOCAL_VAR )
  115.       { /* set type , don't need to patch */
  116.         q->type = CA_EXPR ;
  117.       }
  118.       else
  119.       if ( * q->type_p == ST_LOCAL_ARRAY )
  120.       { q->type = CA_ARRAY ;
  121.         start[q->call_offset].op = LA_PUSHA ;
  122.         /* offset+1 op is OK */
  123.       } 
  124.       else /* try to infer from callee */
  125.       {
  126.         switch( callee->typev[q->arg_num] )
  127.         {
  128.           case  ST_LOCAL_VAR :
  129.                 q->type = CA_EXPR ;
  130.                 * q->type_p = ST_LOCAL_VAR ;
  131.                 /* do not need to patch */
  132.                 break ;
  133.  
  134.           case  ST_LOCAL_ARRAY :
  135.                 q->type = CA_ARRAY ;
  136.                 * q->type_p = ST_LOCAL_ARRAY ;
  137.                 start[q->call_offset].op = LA_PUSHA ;
  138.                 break ;
  139.         }
  140.       }
  141.     }
  142.  
  143.     /* if we still do not know the type put on the new list
  144.        else type check */
  145.  
  146.     if ( q->type == ST_NONE || q->type == ST_LOCAL_NONE )
  147.     {
  148.       q->link = exit_list ;
  149.       exit_list = q ;
  150.     }
  151.     else  /* type known */
  152.     {
  153.       if ( callee->typev[q->arg_num] == ST_LOCAL_NONE )
  154.            callee->typev[q->arg_num] = q->type ;
  155.  
  156.       else
  157.       if ( q->type != callee->typev[q->arg_num] )
  158.       {
  159.         errmsg(0, "line %u: type error in arg(%d) in call to %s",
  160.           line_no, q->arg_num+1, callee->name) ;
  161.         if ( ++compile_error_count == MAX_COMPILE_ERRORS )
  162.                     mawk_exit(1) ;
  163.       }
  164.  
  165.       zfree(q, sizeof(CA_REC)) ;
  166.       check_progress = 1 ;
  167.     }
  168.   } /* while */
  169.  
  170.   return  exit_list ;
  171. }
  172.  
  173.  
  174. static  int  arg_cnt_ok( fbp, q, line_no )
  175.   FBLOCK  *fbp ;
  176.   CA_REC  *q ;
  177.   unsigned line_no ;
  178. {
  179.   if ( q->arg_num  >= fbp->nargs )
  180.   {
  181.     errmsg(0, "line %u: too many arguments in call to %s" ,
  182.        line_no, fbp->name ) ;
  183.     if ( ++compile_error_count == MAX_COMPILE_ERRORS ) 
  184.               mawk_exit(1) ;
  185.  
  186.     return  0 ;
  187.   }
  188.   else  return 1 ;
  189. }
  190.  
  191.  
  192. FCALL_REC  *resolve_list ;
  193.         /* function calls whose arg types need checking 
  194.            are stored on this list */
  195.  
  196.  
  197. /* on first pass thru the resolve list
  198.    we check :
  199.       if forward referenced functions were really defined
  200.       if right number of arguments
  201.    and compute call_start which is now known
  202. */
  203.  
  204. static  FCALL_REC *first_pass( p )
  205.   register FCALL_REC *p ;
  206. { FCALL_REC dummy ;
  207.   register FCALL_REC *q = &dummy ; /* trails p */
  208.  
  209.   q->link = p ;
  210.   while ( p )
  211.   {
  212.     if ( ! p->callee->code )
  213.     { /* callee never defined */
  214.       errmsg(0, "line %u: function %s never defined" ,
  215.           p->line_no, p->callee->name) ;
  216.       if ( ++compile_error_count == MAX_COMPILE_ERRORS ) 
  217.               mawk_exit(1) ;
  218.       /* delete p from list */
  219.       q->link = p->link ;
  220.       /* don't worry about freeing memory, we'll exit soon */
  221.     }
  222.     else  /* note p->arg_list starts with last argument */
  223.     if ( ! p->arg_list  /* nothing to do */  ||
  224.          ! p->arg_cnt_checked && 
  225.          ! arg_cnt_ok(p->callee, p->arg_list, p->line_no) )
  226.     {  q->link = p->link ; /* delete p */ 
  227.        /* the ! arg_list case is not an error so free memory */
  228.        zfree(p, sizeof(FCALL_REC)) ;
  229.     }
  230.     else
  231.     { /* keep p and set call_start */
  232.       q = p ;
  233.       switch ( p->call_scope )
  234.       {
  235.         case SCOPE_MAIN  :
  236.                 p->call_start = main_start ;
  237.                 break ;
  238.  
  239.         case SCOPE_BEGIN :
  240.                 p->call_start = begin_start ;
  241.                 break ;
  242.  
  243.         case SCOPE_END :
  244.                 p->call_start = end_start ;
  245.                 break ;
  246.  
  247.         case SCOPE_FUNCT :
  248.                 p->call_start = p->call->code ;
  249.                 break ;
  250.       }
  251.     }
  252.     p = q->link ;
  253.   }
  254.   return  dummy.link ;
  255. }
  256.  
  257. /* continuously walk the resolve_list making type deductions
  258.    until this list goes empty or no more progress can be made
  259.    (An example where no more progress can be made is at end of file
  260. */
  261.  
  262. void  resolve_fcalls()
  263. { register FCALL_REC *p, *old_list , *new_list ;
  264.   int progress ; /* a flag */
  265.  
  266.   old_list = first_pass(resolve_list) ;
  267.   new_list = (FCALL_REC *) 0 ;
  268.   progress = 0 ;
  269.  
  270.   while ( 1 )
  271.   {
  272.     if ( !(p = old_list) )
  273.     { /* flop the lists */
  274.       if ( !(p = old_list = new_list)  /* nothing left */
  275.           || ! progress    /* can't do any more */ )  return ;
  276.  
  277.       /* reset after flop */
  278.       new_list = (FCALL_REC *) 0 ;  progress = 0 ;
  279.     }
  280.  
  281.     old_list = p->link ;
  282.  
  283.     if ( p->arg_list = call_arg_check(p->callee, p->arg_list ,
  284.               p->call_start, p->line_no)  )
  285.     {
  286.        /* still have work to do , put on new_list   */
  287.        progress  |= check_progress ;
  288.        p->link = new_list ;  new_list = p ;
  289.     }
  290.     else  /* done with p */
  291.     {  progress = 1 ;  zfree(p, sizeof(FCALL_REC)) ; }
  292.   }
  293. }
  294.  
  295. /* the parser has just reduced a function call ;
  296.    the info needed to type check is passed in.  If type checking
  297.    can not be done yet (most common reason -- function referenced
  298.    but not defined), a node is added to the resolve list.
  299. */
  300. void check_fcall( callee, call_scope, call, arg_list, line_no )
  301.   FBLOCK *callee ;
  302.   int call_scope ;
  303.   FBLOCK  *call ;
  304.   CA_REC  *arg_list ;
  305.   unsigned line_no ;
  306. {
  307.   FCALL_REC *p ;
  308.   INST *call_start ;
  309.  
  310.   if ( ! callee->code ) 
  311.   { /* forward reference to a function to be defined later */
  312.     p = (FCALL_REC *) zmalloc(sizeof(FCALL_REC)) ;
  313.     p->callee = callee ;
  314.     p->call_scope = call_scope ;
  315.     p->call = call ;
  316.     p->arg_list = arg_list ;
  317.     p->arg_cnt_checked = 0 ;
  318.     p->line_no = line_no ;
  319.     /* add to resolve list */
  320.     p->link = resolve_list ; resolve_list = p ;
  321.   }
  322.   else
  323.   if ( arg_list && arg_cnt_ok( callee, arg_list, line_no ) )
  324.   {
  325.       switch ( call_scope )
  326.       {
  327.         case SCOPE_MAIN  :
  328.                 call_start = main_start ;
  329.                 break ;
  330.  
  331.         case SCOPE_BEGIN :
  332.                 call_start = begin_start ;
  333.                 break ;
  334.  
  335.         case SCOPE_END :
  336.                 call_start = end_start ;
  337.                 break ;
  338.  
  339.         case SCOPE_FUNCT :
  340.                 call_start = call->code ;
  341.                 break ;
  342.       }
  343.  
  344.       /* usually arg_list disappears here and all is well
  345.          otherwise add to resolve list */
  346.  
  347.       if ( arg_list = call_arg_check(callee, arg_list,
  348.                 call_start, line_no) )
  349.       {
  350.             p = (FCALL_REC *) zmalloc(sizeof(FCALL_REC)) ;
  351.             p->callee = callee ;
  352.             p->call_scope = call_scope ;
  353.             p->call = call ;
  354.             p->arg_list = arg_list ;
  355.             p->arg_cnt_checked = 1 ;
  356.             p->line_no = line_no ;
  357.             /* add to resolve list */
  358.             p->link = resolve_list ; resolve_list = p ;
  359.       }
  360.   }
  361. }
  362.  
  363.  
  364.  
  365. /*  example where typing cannot progress
  366.  
  367. { f(z) }
  368.  
  369. function f(x) { print NR }
  370.  
  371. # this is legal, does something useful, but absurdly written
  372. # We have to design so this works
  373. */
  374.  
  375.